<template>
	<view class="tui-addr-box">
		<form :report-submit="true">
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">收货人</view>
					<input placeholder-class="tui-phcolor" v-model="form.consignee" class="tui-input" name="name"
						placeholder="请输入收货人姓名" maxlength="15" type="text" />
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">手机号码</view>
					<input placeholder-class="tui-phcolor" v-model="form.mobile" class="tui-input" name="mobile"
						placeholder="请输入收货人手机号码" maxlength="11" type="text" />
				</view>
			</tui-list-cell>
			<picker :value="value" mode="multiSelector" @change="picker" @columnchange="columnPicker"
				:range="multiArray">
				<tui-list-cell :arrow="true" padding="0">
					<view class="tui-line-cell">
						<view class="tui-title"><text class="tui-title-city-text">所在城市</text></view>
						<input placeholder-class="tui-phcolor" class="tui-input tui-pr__30" disabled name="city"
							placeholder="请选择城市" maxlength="50" type="text" v-model="form.region" />
					</view>
				</tui-list-cell>
			</picker>
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-title">收货地址</view>
					<input placeholder-class="tui-phcolor" v-model="form.address" class="tui-input" name="address"
						placeholder="请输入详细的收货地址" maxlength="50" type="text" />
				</view>
			</tui-list-cell>
			<!-- 	<tui-list-cell :hover="false" padding="0">
				<view class="tui-line-cell">
					<view class="tui-cell-title">地址类型</view>
					<view class="tui-addr-label">
						<text v-for="(item,index) in lists" :key="index" class="tui-label-item" :class="{'tui-label-active':index==1}">{{item}}</text>
					</view>
				</view>
			</tui-list-cell> -->

			<!-- 默认地址 -->
			<tui-list-cell :hover="false" padding="0">
				<view class="tui-swipe-cell">
					<view>设为默认地址</view>
					<!-- <switch checked color="#19be6b" class="tui-switch-small" /> -->
					<tn-switch v-model="form.is_default" activeColor="#19be6b"></tn-switch>

				</view>
			</tui-list-cell>
			<!-- 保存地址 -->
			<view class="tui-del" v-if="form.id">
				<tui-button shadow type="danger" height="88rpx" shape="circle" @click="editAddress">修改收货地址</tui-button>
			</view>
			<view class="tui-del" v-if="form.id">
				<tui-button shadow type="gray" height="88rpx" shape="circle" @click="delAddress">删除收货地址</tui-button>
			</view>
			<view class="tui-addr-save" v-else>
				<tui-button shadow type="danger" height="88rpx" shape="circle" @click="addAddress">保存收货地址</tui-button>
			</view>
		</form>
	</view>
</template>

<script>
	import { getAddressAdd,getAddressDetail,getAddressDetailEdit,getDataArea,getDataDelete } from "@/apis/index.js"
	export default {
		data() {
			return {
				lists: ["公司", "家", "学校", "其他"],
				selectList: [],
				multiArray: [], //picker数据
				value: [0, 0, 0],
				text: "",
				checked: false,
				form: {
					consignee: "",
					mobile: "",
					address: "",
					is_default: false,
					region: "",
					province_name: "",
					province_id: "",
					city_name: "",
					city_id: "",
					district_name: "",
					district_id: ""
				}
			}
		},
		async onLoad(option) {
			if (option.id) {
				this.getDetail(option.id)
			}
		
		
			this.getAreaList()
		},
		methods: {
			
			delAddress(){
				getDataDelete(this.form.id).then(res=>{
					if (res.code==1) {
						uni.navigateBack({
							delta:1,
							success() {
								uni.$emit('update',{msg:true})
							}
						})
					}
				})
			},
			//获取城市
			getAreaList(){
				getDataArea().then(res=>{
					this.selectList=res.data
					this.multiArray = [
						this.toArr(this.selectList),
						this.toArr(this.selectList[0].children),
						this.toArr(this.selectList[0].children[0].children)
					]
				})
			},
			//获取地址
			async getDetail(id){
				let res= await getAddressDetail(id)
				this.form=res.data
				// this.form.region=res.data
				
				this.form.region = res.data.province_name + " " + res.data.city_name + " " + res.data.district_name;
			},
			
			//修改地址
			
			async editAddress(){
				if (!this.form.consignee) {
					uni.showToast({
						title:'请填写收货人'
					})
					return
				}
				if (!this.form.mobile) {
					uni.showToast({
						title:'请填写手机号'
					})
					return
				}
				if (!this.form.region) {
					uni.showToast({
						title:'请填写所在城市'
					})
					return
				}
				if (!this.form.address) {
					uni.showToast({
						title:'请填写收货地址'
					})
					return
				}
				getAddressDetailEdit(this.form.id,this.form).then(res=>{
					if (res.code==1) {
						uni.navigateBack({
							delta:1,
							success() {
								uni.$emit('update',{msg:true})
							}
						})
					}
					
				})
				
				
			},
			
			
			
			//保存
			addAddress(){
				if (!this.form.consignee) {
					uni.showToast({
						title:'请填写收货人'
					})
					return
				}
				if (!this.form.mobile) {
					uni.showToast({
						title:'请填写手机号'
					})
					return
				}
				if (!this.form.region) {
					uni.showToast({
						title:'请填写所在城市'
					})
					return
				}
				if (!this.form.address) {
					uni.showToast({
						title:'请填写收货地址'
					})
					return
				}
				getAddressAdd(this.form).then(res=>{
					if (res.code==1) {
						uni.navigateBack({
							delta:1,
							success() {
								uni.$emit('update',{msg:true})
							}
						})
					}
					
				})
			},
			picker: function(e) {
				let id = e.detail.value;
				if (this.selectList.length > 0) {
					let provice = this.selectList[id[0]].name
					let city = this.selectList[id[0]].children[id[1]].name
					let district = this.selectList[id[0]].children[id[1]].children[id[2]].name
					// this.selectList[id[0]].children[id[1]].children[id[2]].value
					this.form.region = provice + " " + city + " " + district;
					// return
					this.form.province_name=this.selectList[id[0]].name
					this.form.province_id= this.selectList[id[0]].value
					this.form.city_name=this.selectList[id[0]].children[id[1]].name
					this.form.city_id= this.selectList[id[0]].children[id[1]].value
					this.form.district_name= this.selectList[id[0]].children[id[1]].children[id[2]].name
					this.form.district_id= this.selectList[id[0]].children[id[1]].children[id[2]].id
					
					
					this.$forceUpdate()
					
				}
			},
			toArr(object) {
				let arr = [];
				for (let i in object) {
					arr.push(object[i].name);
				}
				return arr;
			},
			columnPicker: function(e) {
				//第几列 下标从0开始
				let column = e.detail.column;
				//第几行 下标从0开始
				let value = e.detail.value;
				if (column === 0) {
					this.multiArray = [
						this.multiArray[0],
						this.toArr(this.selectList[value].children),
						this.toArr(this.selectList[value].children[0].children)
					];
					this.value = [value, 0, 0]
				} else if (column === 1) {
					this.multiArray = [
						this.multiArray[0],
						this.multiArray[1],
						this.toArr(this.selectList[this.value[0]].children[value].children)
					];
					this.value = [this.value[0], value, 0]
				}
			}
		}
	}
</script>

<style>
	.tui-addr-box {
		padding: 20rpx 0;
	}

	.tui-line-cell {
		width: 100%;
		padding: 24rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-title {
		width: 180rpx;
		font-size: 28rpx;
		flex-shrink: 0;
	}

	.tui-title-city-text {
		width: 180rpx;
		height: 40rpx;
		display: block;
		line-height: 46rpx;
	}

	.tui-input {
		width: 500rpx;
	}

	.tui-pr__30 {
		padding-right: 30rpx;
	}

	.tui-input-city {
		flex: 1;
		height: 40rpx;
		font-size: 28rpx;
		padding-right: 30rpx;
	}

	.tui-phcolor {
		color: #ccc;
		font-size: 28rpx;
	}

	.tui-cell-title {
		font-size: 28rpx;
		flex-shrink: 0;
	}

	.tui-addr-label {
		margin-left: 70rpx;
	}

	.tui-label-item {
		width: 76rpx;
		height: 40rpx;
		border: 1rpx solid rgb(136, 136, 136);
		border-radius: 6rpx;
		font-size: 26rpx;
		text-align: center;
		line-height: 40rpx;
		margin-right: 20rpx;
		display: inline-block;
		transform: scale(0.92);
	}

	.tui-label-active {
		background: #E41F19;
		border-color: #E41F19;
		color: #fff;
	}

	.tui-swipe-cell {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #fff;
		padding: 10rpx 24rpx;
		box-sizing: border-box;
		border-radius: 6rpx;
		overflow: hidden;
		font-size: 28rpx;
	}

	.tui-switch-small {
		transform: scale(0.8);
		transform-origin: 100% center;
	}

	/* #ifndef H5 */
	.tui-switch-small .wx-switch-input {
		margin: 0 !important;
	}

	/* #endif */

	/* #ifdef H5 */
	>>>uni-switch .uni-switch-input {
		margin-right: 0 !important;
	}

	/* #endif */

	.tui-addr-save {
		padding: 24rpx 30rpx;
		margin-top: 100rpx;
	}

	.tui-del {
		padding: 24rpx 30rpx;
	}
</style>